package com.enjoy.dome.dao;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.target.CustomTarget;
import com.enjoy.dome.R;
import com.enjoy.dome.app.MyApp;


public class ImgDown {

    public static void setImage(Context context, ImageView iv, String resurl) {
        if (resurl.startsWith("res:")) {
            int id = context.getResources().getIdentifier(resurl.substring(4), "drawable", context.getPackageName());
            iv.setImageResource(id);
        }
        if (resurl.startsWith("url:")) {
            ImgDown.load( iv, resurl.substring(4));
        } else if (resurl.startsWith("http")) {
            ImgDown.load( iv, resurl.substring(4));
        }
    }
    public static void load(ImageView iv, String url) {
        if (TextUtils.isEmpty(url)){
            iv.setImageResource(R.drawable.ic_launcher_background);
            return;
        }
        Glide.with(MyApp.getInstance())
                .asBitmap()
                .load(url)
                .into(iv);
    }
    public static void load(String url, CustomTarget target) {
        if (TextUtils.isEmpty(url)){
           return;
        }
        Glide.with(MyApp.getInstance())
                .asBitmap()
                .load(url)
                .into(target);
    }

    public static void load(ImageView iv, String url, int placeholderResId, int errorResId) {
        if (TextUtils.isEmpty(url)){
            iv.setImageResource(errorResId);
            return;
        }
        RequestBuilder builder=Glide.with(MyApp.getInstance())
                .asBitmap()
                .load(url);
        if (placeholderResId > 0) {
            builder.placeholder(placeholderResId);
        }
        if (errorResId > 0) {
            builder.error(errorResId);
        }
        builder.into(iv);
    }
}